package org.internna.ossmoney.model.budget;

import java.util.Locale;
import java.util.Currency;
import java.math.BigDecimal;
import javax.persistence.ManyToOne;
import javax.persistence.FetchType;
import javax.validation.constraints.NotNull;
import org.hibernate.annotations.Type;
import org.internna.ossmoney.model.Subcategory;
import org.internna.ossmoney.model.budget.MonthlyBudget;
import org.springframework.roo.addon.equals.RooEquals;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.roo.addon.jpa.entity.RooJpaEntity;
import org.springframework.roo.addon.serializable.RooSerializable;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;

@RooEquals
@RooJavaBean
@RooToString
@RooJpaEntity
@RooSerializable
@RooJpaActiveRecord
public class CategoryBudget implements Comparable<CategoryBudget> {

    private static final long serialVersionUID = -8398958630951745386L;

    @NotNull
    private BigDecimal expectedAmount;

    @NotNull
    @ManyToOne(fetch = FetchType.EAGER)
    private Subcategory category;

    @NotNull
    @ManyToOne(fetch = FetchType.EAGER)
    private MonthlyBudget monthlyBudget;

    @NotNull
    @Type(type = "org.hibernate.type.LocaleType")
    private Locale locale;

    private transient BigDecimal expended;

    @Override
    public int compareTo(CategoryBudget other) {
        return other == null ? 1 : expectedAmount == null ? other.expectedAmount == null ? 0 : -1 : other.expectedAmount == null ? 1 : expectedAmount.compareTo(other.expectedAmount);
    }

    public final String getCurrency() {
        Currency accountCurrency = Currency.getInstance(getLocale());
        return accountCurrency.getCurrencyCode();
    }

	public final BigDecimal getExpended() {
		return expended;
	}

	public final void setExpended(BigDecimal expended) {
		this.expended = expended;
	}

	public final boolean isNegative() {
		return expended.doubleValue() > expectedAmount.doubleValue();
	}
}
